import common.CommonDemo;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.time.Duration;

/**
 * 操作提示用户输入的对话框
 */
public class Prompt {

    private CommonDemo commonDemo;
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeTest
    public void setUp() throws Exception{
        commonDemo = new CommonDemo();
        driver = commonDemo.getChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(2));
    }
    @Test
    public void test() throws Exception{
        String url = "src/main/resources/Prompt.html";
        String filepath = commonDemo.getUserDir(url);
        driver.get(filepath);
        driver.manage().window().maximize();
        WebElement element = driver.findElement(By.name("prompt"));
        wait.until(ExpectedConditions.elementToBeClickable(element)).click();
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        Thread.sleep(2000);
        alert.sendKeys("ABC");
        alert.accept();
    }
    @AfterTest
    public void tearDown() throws Exception{
        Thread.sleep(2000);
        driver.quit();
    }
}
